package com.example.note.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.note.R;
import com.example.note.activity.DiaryEditActivity;
import com.example.note.database.DiaryEntity;

import java.util.ArrayList;
import java.util.List;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.DiaryViewHolder> {
    
    private Context context;
    private List<DiaryEntity> diaries;
    private OnDiaryClickListener onDiaryClickListener;
    
    public interface OnDiaryClickListener {
        void onDiaryClick(DiaryEntity diary);
        void onDiaryLongClick(DiaryEntity diary);
    }
    
    public DiaryAdapter(Context context) {
        this.context = context;
        this.diaries = new ArrayList<>();
    }
    
    public void setOnDiaryClickListener(OnDiaryClickListener listener) {
        this.onDiaryClickListener = listener;
    }
    
    public void setDiaries(List<DiaryEntity> diaries) {
        this.diaries = diaries;
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public DiaryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_diary, parent, false);
        return new DiaryViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull DiaryViewHolder holder, int position) {
        DiaryEntity diary = diaries.get(position);
        holder.bind(diary);
    }
    
    @Override
    public int getItemCount() {
        return diaries.size();
    }
    
    class DiaryViewHolder extends RecyclerView.ViewHolder {
        
        private TextView tvTitle, tvDate, tvContent, tvTimestamp;
        private ImageView ivMood, ivWeather, ivImagePreview;
        
        public DiaryViewHolder(@NonNull View itemView) {
            super(itemView);
            
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvTimestamp = itemView.findViewById(R.id.tvTime);
            ivMood = itemView.findViewById(R.id.ivMood);
            ivWeather = itemView.findViewById(R.id.ivWeather);
            ivImagePreview = itemView.findViewById(R.id.ivPreview);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && onDiaryClickListener != null) {
                    onDiaryClickListener.onDiaryClick(diaries.get(position));
                }
            });
            
            itemView.setOnLongClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && onDiaryClickListener != null) {
                    onDiaryClickListener.onDiaryLongClick(diaries.get(position));
                    return true;
                }
                return false;
            });
        }
        
        public void bind(DiaryEntity diary) {
            tvTitle.setText(diary.getTitle());
            tvDate.setText(diary.getDate());
            
            // 显示内容预览（最多显示100个字符）
            String content = diary.getContent();
            if (content.length() > 100) {
                content = content.substring(0, 100) + "...";
            }
            tvContent.setText(content);
            
            // 显示时间戳
            tvTimestamp.setText(diary.getTime());
            
            // 显示心情图标
            switch (diary.getMood()) {
                case "1":
                    ivMood.setImageResource(R.drawable.ic_mood_happy);
                    break;
                case "2":
                    ivMood.setImageResource(R.drawable.ic_mood_sad);
                    break;
                case "3":
                    ivMood.setImageResource(R.drawable.ic_mood_neutral);
                    break;
                default:
                    ivMood.setImageResource(R.drawable.ic_mood_happy);
                    break;
            }
            
            // 显示天气图标
            switch (diary.getWeather()) {
                case "1":
                    ivWeather.setImageResource(R.drawable.ic_weather_sunny);
                    break;
                case "2":
                    ivWeather.setImageResource(R.drawable.ic_weather_cloudy);
                    break;
                case "3":
                    ivWeather.setImageResource(R.drawable.ic_weather_rainy);
                    break;
                default:
                    ivWeather.setImageResource(R.drawable.ic_weather_sunny);
                    break;
            }
            
            // 显示图片预览
            if (!TextUtils.isEmpty(diary.getImagePath())) {
                ivImagePreview.setVisibility(View.VISIBLE);
                try {
                    ivImagePreview.setImageURI(Uri.parse(diary.getImagePath()));
                } catch (Exception e) {
                    // 如果URI解析失败，隐藏图片预览
                    ivImagePreview.setVisibility(View.GONE);
                }
            } else {
                ivImagePreview.setVisibility(View.GONE);
            }
        }
    }
}